
#include "../../Command.h"

#include "../../engine.h"

#include "../../User.h"

#include "../../NLogic/Static.h"
#include "../../NLogic/Buff.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_PK_USER_LIST(User * pUser, ::Network::Packet * pPacket)
{
/*
	C -> S
	/// PK 매치 정보
	CS_REQ_PK_USER_LIST	0x1406
	{
		u64	u64pk_match_seq;		// 매치 시퀀스
	}

	S -> C
	/// 본인인 앞으로 해서 내려주심 좋고.. 아님 클라에서 별도 소트 필요
	SC_REQ_PK_USER_LIST	0x1407
	{
		u8	u8list_count;
		{
			str				struser_id;		 		//상대방 ID
			u32				u32gid;					//gid
			u8				u8class_type;
			s8				s8pet_type;				//페어리 (0: 기본)
			u16				u16level;				//레벨
			u32				u32win_score;				// 승리 횟수? 점수?

			str				str_ImageUrl;				// 친구 사진 URL
			u8				u8f_buff;				// 친구 버프
			u8				u8s_buff;				// 승점 버프
			u8				u8g_buff;				// 길드

			str				strWin_Message;			// pk 도발 멘트
		}
	}
*/
	STRESSLOG("%s", __FUNCTION__);

	NLogic::Space * pSpace = pUser->GetSpace();
	if( NULL == pSpace)
	{
		NLogic::NFunction::SendExceptionNoSpace(pUser);
		DEBUGLOG("%s %d Space is Null user_[%d]", __FUNCTION__, __LINE__, pUser->GetUserSeq());
		return;
	}

	if( pSpace->GetBattleTree().empty() )
	{
		NLogic::NFunction::SendExceptionNoSpace(pUser);
		DEBUGLOG("%s %d pSpace->GetBattleTree().empty() user_[%d]", __FUNCTION__, __LINE__, pUser->GetUserSeq());
		return;
	}

	NLogic::Buff * pBuff = pUser->GetBuff();
	if( NULL == pBuff )
		return;

	NLogic::Buff::Data * pFriendBuff = pBuff->GetData(NLogic::Buff::BuffType::FRIEND);
	if( NULL == pFriendBuff )
		return ;

	NLogic::Buff::Data * pWinPointBuff = pBuff->GetData(NLogic::Buff::BuffType::WINPOINT);
	if( NULL == pWinPointBuff )
		return ;

	NLogic::Buff::Data * pGuildBuff = pBuff->GetData(NLogic::Buff::BuffType::GUILD);
	if( NULL == pGuildBuff )
		return ;

	PREPARE_PACKET;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << static_cast<uint8_t>(pSpace->GetBattleTree().size());

	*pAck << pUser->GetUserInfo().nickname.c_str();
	*pAck << pUser->GetUserSeq();
	*pAck << static_cast<uint8_t>(pUser->GetCharacterInfo().classtype);
	*pAck << static_cast<uint8_t>(0);	// pettype
	*pAck << static_cast<uint16_t>(pUser->GetCharacterInfo().level);
	*pAck << static_cast<uint32_t>(pUser->GetCharacterInfo().ep_current);
	*pAck << pUser->GetUserInfo().profile_url.c_str();

	time_t curtime = time(NULL);

	if( pFriendBuff->_regdate <= curtime )
		*pAck << static_cast<uint8_t>(0);	// friend buff
	else
		*pAck << static_cast<uint8_t>(1);	// friend buff

	if( pWinPointBuff->_regdate <= curtime )
		*pAck << static_cast<uint8_t>(0);	// winpoint buff
	else
		*pAck << static_cast<uint8_t>(1);	// winpoint buff

	if (0 >= pUser->GetUserInfo().guild_seq || 0 >= pGuildBuff->u16skill_tid)
		*pAck << static_cast<uint8_t>(0);	// guild buff
	else
		*pAck << static_cast<uint8_t>(1);	// guild buff

	*pAck << pUser->GetCharacterInfo().win_comment.c_str();

	auto itr = pUser->GetSpace()->GetBattleTree().begin();
	for ( ; itr != pUser->GetSpace()->GetBattleTree().end() ; ++itr )
	{
		NLogic::Battle * pBattle = itr->second;
		if (pBattle == pUser->GetBattleRoyalBattle())
			continue;

		*pAck << pBattle->GetData().nickname.c_str();
		*pAck << pBattle->GetData().userseq;
		*pAck << static_cast<uint8_t>(pBattle->GetData().classtype);
		*pAck << static_cast<uint8_t>(0);	// pettype
		*pAck << static_cast<uint16_t>(pBattle->GetData().level);
		*pAck << static_cast<uint32_t>(pBattle->GetData().ep_current);
		*pAck << pBattle->GetData().profile_url.c_str();

		User * pLocalUser = pBattle->GetUser();
		if( NULL == pLocalUser )
		{
			*pAck << static_cast<uint8_t>(0);	// friend buff
			*pAck << static_cast<uint8_t>(0);	// winpoint buff
			*pAck << static_cast<uint8_t>(0);	// guild buff
		}
		else
		{
			NLogic::Buff * pBuff = pLocalUser->GetBuff();
			if( NULL == pBuff )
			{
				::Network::GETPACKETPOOL()->SEND_DEL(pAck);
				return;
			}

			// 친구 버프
			NLogic::Buff::Data * pFriendBuff = pBuff->GetData(NLogic::Buff::BuffType::FRIEND);
			if( NULL == pFriendBuff )
			{
				::Network::GETPACKETPOOL()->SEND_DEL(pAck);
				return ;
			}

			if( pFriendBuff->_regdate <= curtime )
				*pAck << static_cast<uint8_t>(0);	// friend buff
			else
				*pAck << static_cast<uint8_t>(1);	// friend buff

			// 승점 버프
			NLogic::Buff::Data * pWinPointBuff = pBuff->GetData(NLogic::Buff::BuffType::WINPOINT);
			if( NULL == pWinPointBuff )
			{
				::Network::GETPACKETPOOL()->SEND_DEL(pAck);
				return ;
			}

			if( pWinPointBuff->_regdate <= curtime )
				*pAck << static_cast<uint8_t>(0);	// winpoint buff
			else
				*pAck << static_cast<uint8_t>(1);	// winpoint buff

			// 길드 버프
			NLogic::Buff::Data * pGuildBuff = pBuff->GetData(NLogic::Buff::BuffType::GUILD);
			if (NULL == pGuildBuff)
			{
				::Network::GETPACKETPOOL()->SEND_DEL(pAck);
				return ;
			}

			if (0 >= pLocalUser->GetUserInfo().guild_seq || 0 >= pGuildBuff->u16skill_tid)
				*pAck << static_cast<uint8_t>(0);	// guild buff
			else
				*pAck << static_cast<uint8_t>(1);	// guild buff
		}

		*pAck << pBattle->GetData().win_comment.c_str();
	}

	pAck->MakePacket(CMD_SC_REQ_PK_USER_LIST);

	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
